<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
//	protected function _initDb() {
//		
//		$dbOption = $this->getOption('resources');
//    	$dbOption = $dbOption['db'];
////    	zend_debug::dump($dbOption);
//        // Setup database
//        $db = Zend_Db::factory($dbOption['adapter'],$dbOption['params']);
//        $db->setFetchMode(Zend_Db::FETCH_ASSOC);
//        $db->query("SET NAMES 'utf8'");
//        $db->query("SET CHARACTER SET 'utf8'");       
//
//        Zend_Registry::set('connectDB',$db);
//		
//        //Khi thiet lap che do nay model moi co the su dung duoc
//        Zend_Db_Table::setDefaultAdapter($db);
//        Zend_Db_Table_Abstract::setDefaultAdapter($db);
//        
//                //Caching
//		$frontendOptions = array(
//		      'lifetime'     => 25200,
//		         'automatic_serialization' => true
//		         );
//		$backendOptions  = array(
//		          'cache_dir'                => APPLICATION_PATH . '/tmp'
//		         );
//		$cache = Zend_Cache::factory(
//		      'Core',
//		                     'File',
//		                     $frontendOptions,
//		                     $backendOptions
//		                     );
//		//Cache table metadata
//		Zend_Db_Table_Abstract::setDefaultMetadataCache($cache);
//        // Return it, so that it can be stored by the bootstrap
//        return $db;
//	}
    
    // khoi tao site map
	protected function _initSetConstants(){
	
		$config = parse_ini_file(APPLICATION_PATH . '/configs/constant.ini');
		foreach ($config as $key => $value){
            if(!defined($key)){
                define($key, $value);
            }
		}
	}
 	protected function _initController ( )
    {
        $front = Zend_Controller_Front::getInstance();
        $front->setControllerDirectory( array (
        	'default' => APPLICATION_PATH . '/modules/Default/controllers',
        ));

        return $front;
    }
	protected function _initLog() {
		$options = $this->getOption('resources');
 
		$partitionConfig = $this->getOption('log');
		$logOptions = $options['log'];
 
		$baseFilename = $logOptions['stream']['writerParams']['stream'];
		if ($partitionConfig['partitionStrategy'] == 'context'){
			$baseFilename = $partitionConfig['path'].'/application';
		}
		$logFilename = '';
		switch(strtolower($partitionConfig['partitionFrequency'])){
			case 'daily':
				$logFilename = $baseFilename.'_'.date('Y_m_d') . ".log";
				break;
 
			case 'weekly':
				$logFilename = $baseFilename.'_'.date('Y_W') . ".log";
				break;
 
			case 'monthly':
				$logFilename = $baseFilename.'_'.date('Y_m') . ".log";
				break;
 
			case 'yearly':
				$logFilename = $baseFilename.'_'.date('Y') . ".log";
				break;
			default:
                $logFilename = $baseFilename . ".log";;
 
		}
 
		$logOptions['stream']['writerParams']['stream'] = $logFilename;
 
		$logger = Zend_Log::factory($logOptions);
		Zend_Registry::set('logger', $logger);
 
        return $logger;
    }
}

